/* revised based on gds.c from libMaskv6. Add gds_ for each function
 * revised by Yongbo Tang, 20121108, for pylibMask.
 *
 * Low-level routines to write the GDSII file.
 *
 * Copyright (c) 2010 Xaveer Leijtens
 *
 * This file is part of libMask.
 *
 * libMask is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * libMask is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with libMask.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "gds.h"

extern void angle_vector(double, int*, int*);
extern double map_0_2pi(double);

/* Set the current layer */
void gds_layer(int n)
{
  layernr(n);
  clayer=n;
}

void gds_mskpolygon3(int l, double x0, double y0, double x1, double y1,
    double x2, double y2)
{
  double x[3], y[3];
  x[0] = x0; y[0] = y0;
  x[1] = x1; y[1] = y1;
  x[2] = x2; y[2] = y2;

  if (drawlayer(l)) {
    layer(l);
    gds_polygon(3, x, y);
  }
}

void gds_mskpolygon4(int l, double x0, double y0, double x1, double y1,
    double x2, double y2, double x3, double y3)
{
  double x[4], y[4];
  x[0] = x0; y[0] = y0;
  x[1] = x1; y[1] = y1;
  x[2] = x2; y[2] = y2;
  x[3] = x3; y[3] = y3;

  if (drawlayer(l)) {
    layer(l);
    gds_polygon(4, x, y);
  }
}

void gds_mskpolygonN(int l, double *x, double *y, int n)
{
  if (drawlayer(l)) {
    layer(l);
    gds_polygon(n, x, y);
  }
}

/* This routine uses three point on the arc to define it. For the CIF
 * routine, a CleWin specific extension is used, where the arc definition
 * is in a CIF comment. It is interpreted by clewin and written as polygon
 * (with course sampling!) to both CIF or GDS output files. In the CIF file
 * the CleArcWire is maintained and takes preference over the polygon that
 * follows. Bit of a cludge, and used primarily for debugging. It may also
 * be useful for Raith extensions (e.g. for fixed beam moving stage (FBMS)
 * structures.
 */
void gds_mskarc(int l, double w, double x1, double y1, double x2,
    double y2, double x3, double y3)
{
  fprintf(stderr, "Error: mskarc (GDSII) not implemented (but continuing...)\n");
}

void gds_mskarc2 (int l, double w, double r, double a1, double a2, double x1,
    double y1)
{
  fprintf(stderr, "Error: mskarc2 (GDSII) not implemented (but continuing...)\n");
}

void gds_mskcircle(int l, double w, double x1, double y1, double x2, double y2)
{
  fprintf(stderr, "Error: mskcircle (GDSII) not implemented (but continuing...)\n");
}

double p_x[2000], p_y[2000];
int np;
double path_width;

/* polyline, wire */
void gds_mskpolystart(int l, double w)
{
  layer(l);
  np = 0;
  path_width = w;
}

void gds_mskpolypoint(int l, double x, double y)
{
  p_x[np]   = x;
  p_y[np++] = y;
}

void gds_mskpolygonend(int l)
{
  if (drawlayer(l)) {
    layer(l);
    gds_polygon(np, p_x, p_y);
  }
}

void gds_mskpolyend(int l)
{
  if (drawlayer(l)) {
    layer(l);
    gds_polyline(np, p_x, p_y, path_width);
  }
}

void gds_mskpolygonstart(int l)
{
  layer(l);
  np = 0;
}

void gds_msk_main_header(const char *filename)
{
  clayer = layers->number; /* set default layer to first layer defined */
  if (!strcmp(filename, "")) {
    MASK = stdout;
  //the file has to be open by wb or it will lead to error (occasionally one more byte is wriitten into the file) if compiled by mingw.
  } else if ((MASK = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "Can't open '%s' for writing.\nError %d\n",
      filename, errno);
    exit(errno);
  }
  gds_header();
  gds_bgnlib();
  gds_libname(filename);
  gds_units();
}

void gds_msk_sublayout_header(const char *layout)
{
  gds_bgnstr();
  gds_strname(layout);
}

void gds_msk_sublayout_trailer()
{
  gds_nodata(GDS_REC_ENDSTR);
}

void gds_msk_trailer()
{
  gds_msk_sublayout_trailer();
  gds_nodata(GDS_REC_ENDLIB);
  fclose(MASK);
}

void gds_mskline(int l, double w, double xb, double yb, double xe, double ye)
{
  gds_mskpolystart(l, w);
  gds_mskpolypoint(l, xb, yb);
  gds_mskpolypoint(l, xe, ye);
  gds_mskpolyend(l);
}

void gds_msklayout(const int layoutnr, const char *name, double a, double x,
    double y, int flip)
{
  gds_nodata(GDS_REC_SREF);
  gds_sname(name);
  if (fabs(a) > 0 || flip) {
    /* insert TRANS record */
    if (flip) {
      gds_strans(GDS_BIT_REFLECT);
    } else {
      gds_strans(0);
    }
    if (fabs(a) > 0) {
      gds_angle(DEG(a));
    }
  }
  gds_xy(1, &x, &y, false);
  gds_nodata(GDS_REC_ENDEL);
}
